<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let arr = [1, 1, 'true', 'true', true, true, '15', 15, 15, false, false, undefined, undefined, null, null, NaN, NaN, 'NaN', 'NaN', 0, 0, 'a', 'a', {}, {}];

    // set数据结构去重(不能去重Object)
    function handle(arr) {
      return [...new Set(arr)]
    }

    console.log(handle(arr));

    // 利用对象属性唯一性(可以去重对象)，推荐
    function handle1(arr) {
      let obj = {}
      let newArr = []
      for (let i = 0; i < arr.length; i++) {
        if (!obj[typeof arr[i] + arr[i]]) {
          obj[typeof arr[i] + arr[i]] = 1
          newArr.push(arr[i])
        }
      }
      return newArr
    }
    console.log(handle1(arr));

    // 利用Map去重(不能去重Object)
    function handle2(arr) {
      let obj = new Map()
      let newArr = []
      for (let i = 0; i < arr.length; i++) {
        if (!obj.has(arr[i])) {
          obj.set(arr[i])
          newArr.push(arr[i])
        }
      }
      return newArr
    }
    console.log(handle2(arr));

    // 2. 数组对象去重——reduce遍历+对象属性唯一性
    let arrObj = [
      { name: 'zs' },
      { name: 'zs' },
      { name: 'zs' },
      { name: 'ls' }
    ]
    function handleArr2(arr) {
      let newObj = {}
      let arr1 = arr.reduce((pre, cur) => {
        // newObj[cur.name] ? '' : newObj[cur.name] = pre.push(cur)
        if (!newObj[cur.name]) {
          newObj[cur.name] = 1
          pre.push(cur)
        }
        return pre
      }, [])
      return arr1
    }
    let arr3 = handleArr2(arrObj)
    console.log(arr3);
  </script>
</body>

</html>